<%@tag pageEncoding="utf-8"%>
<%@tag trimDirectiveWhitespaces="true"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@attribute name="preId" required="false" type="java.lang.String"%>
<%@attribute name="totalPage" required="true" type="java.lang.Integer"%>
<%@attribute name="currentPage" required="true" type="java.lang.Integer"%>
<%@attribute name="pagelink" required="false" type="java.lang.String"%>
<%@attribute name="formId" required="false" type="java.lang.String"%>
<c:if test="${totalPage > 1 || currentPage > 1}">
	<%-- 为了防止页面重复引入分页，导致id不一致，所以使用pre进行区分，这个可以进行传入，如果不传入则使用当前时间。 --%>
	<jsp:useBean id="now" class="java.util.Date" />
	<fmt:formatDate var="nowString" value="${now }" pattern="yyyyMMddHHmmssS" />
	<c:choose>
		<c:when test="${empty preId }">
			<c:set var="pre" value="${nowString }" />
		</c:when>
		<c:otherwise>
			<c:set var="pre" value="${preId }" />
		</c:otherwise>
	</c:choose>

	<nav id="${pre}pageDiv">
		<ul class="pagination">
			<li class="${currentPage == 1 ? 'disabled':'' }">
				<a id="${pre}GoPrePageBtn" href="javascript:;">
					<span aria-hidden="true">&laquo;</span><span class="sr-only">Previous</span>
				</a>
			</li>

			<c:forEach begin="1" step="1" end="${totalPage }" var="pageNum">
				<li class="${currentPage == pageNum?'active':''}">
					<a href="javascript:;" class="${pre}pageBtn">${pageNum }</a>
				</li>
			</c:forEach>

			<li class="${currentPage == totalPage ? 'disabled':'' }">
				<a id="${pre}GoNextPageBtn" href="javascript:;">
					<span aria-hidden="true">&raquo;</span><span class="sr-only">Next</span>
				</a>
			</li>
		</ul>

		<c:set var="inputFormProperty" value="" />
		<c:if test="${not empty formId }">
			<c:set var="inputFormProperty" value="form='${formId }'" />
		</c:if>
		<input type="hidden" ${inputFormProperty} id="${pre}page" name="page" value="${currentPage}" />
	</nav>

	<script type="text/javascript">
		$().ready(function() {
			var currentPage = new Number("${currentPage}");
			var totalPage = new Number("${totalPage}");
			var formId = "${formId}";
			var pagelink = "${pagelink}";
			
			function goToPage(page) {
				if(!page || page < 1 || page > totalPage || page == currentPage || isNaN(page)){
					return;
				}
				$("#${pre}page").val(page);
				if(formId){
					$("#" + formId).submit();
				} else if(pagelink) {
					var pieces = pagelink.split("?");
					var action = pieces.shift(0);					
					var queryString = pieces.join("?");
					
					var form = "<form action='" + action +"'>";
					form += "<input type='hidden' name='page' value='" + page + "'/>";
					var paramPairs = queryString.split("&");
					for(var i = 0; i < paramPairs.length; i++){
						var pair = paramPairs[i].split("=");
						var key = pair.shift(0);
						if(!key) {
							continue;
						}						
						var val = pair.join("=");
						form += "<input type='hidden' name='" + key + "' value='" + val + "'/>";	
					}
					form += "</form>";
					
					$(form).appendTo("#${pre}pageDiv").submit();
				} else {
					$("#${pre}page").parents("form").submit();
				}
			}

			
			$("#${pre}GoPrePageBtn").click(function(){
				goToPage(currentPage - 1);
			});
			
			$("#${pre}GoNextPageBtn").click(function(){
				goToPage(currentPage + 1);
			});
			
			$("#${pre}GoToBtn").click(function(){
				var page = $("#${pre}pageInput").val();
				goToPage(page);
			});
			
			$(".${pre}pageBtn").click(function(){
				var page = $(this).text();
				goToPage(page);
			});
			
			
		});
	</script>
</c:if>
